#include "stdafx.h"
#include "glwidget.h"
#include "coresettings.h"
#include <demo/tileengine.h>
#include "gui/choosemaptypedialog.h"

#include <stdio.h>

#include <QtCore/QTimer>
#include <QtGui/QPainter>
#include <QtGui/QKeyEvent>
#include <QtCore/QTimerEvent>
#include <QtGui/QFileDialog>

#include <math.h>

namespace Core {

GLWidget::GLWidget(QWidget *parent)
    :QGLWidget(parent)
    ,_engine(0)
    ,_ticksCount(0)
    ,_ticksSummary(0)
{
    CoreSettings *settings = CoreSettings::instance();
    setFixedSize(settings->windowWidth(), settings->windowHeight());
    setAutoFillBackground(false);

    _time.start();
}

GLWidget::~GLWidget()
{
    if (_ticksCount != 0)
    {
        myDebug() << "Average FPS: "
                  << floor(1000.0 * _ticksCount / _ticksSummary)
                  << "\n";
    }
}

void GLWidget::resetEngine(IEngine *engine)
{
    _engine = engine;
    _engine->initialization();
}

void GLWidget::animate()
{
    updateGL();
   // QTimer* timer = qobject_cast<QTimer*>(sender());
   // _elapsed = (_elapsed + timer->interval()) % 1000;
}

void GLWidget::createMap()
{
    auto engine = dynamic_cast<Demo::TileEngine *>(_engine);
    if (engine) {
        engine->createMap();
    }
}

void GLWidget::loadMap()
{
    auto engine = dynamic_cast<Demo::TileEngine *>(_engine);
    if (engine) {
        auto path = QFileDialog::getOpenFileName(
                    this, tr("Select saved map in XML format"),
                    QString("maps"), QString("*.xml"));
        engine->loadMap(path);
    }
}

void GLWidget::chooseMapGenerationMethod()
{
    auto engine = dynamic_cast<Demo::TileEngine *>(_engine);
    if (engine) {
        ChooseMapTypeDialog * dialog = new ChooseMapTypeDialog();
        dialog->show();

        if (dialog->getMapTypeId() == 1)
        {
            std::cout << "Map type changed to 1" << std::endl;
        }
        if (dialog->getMapTypeId() == 2)
        {
            std::cout << "Map type changed to 2" << std::endl;
        }

    }
}

void GLWidget::saveMap() const
{
    auto engine = dynamic_cast<Demo::TileEngine *>(_engine);
    if (engine) {
        auto path = QFileDialog::getSaveFileName(
                    (QWidget*)0, tr("Select folder and name"));
        engine->saveMap(path);
    }
}

void GLWidget::initializeGL()
{
    glClearColor(0.0, 0.0, 0.0, 1);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glEnable(GL_MULTISAMPLE);

    emit glInitialized();
}

void GLWidget::paintGL()
{
    //! FIXME: too simplified check
    int elapsed = _time.elapsed();
    if (elapsed < 100)
    {
        ++_ticksCount;
        _ticksSummary += elapsed;
    }
    _time.restart();
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    _engine->draw();
}

void GLWidget::resizeGL(int width, int height)
{
    glViewport(0, 0, width, height);
}

void GLWidget::keyPressEvent(QKeyEvent *event)
{
    _engine->onKeyDown(event);
}

void GLWidget::keyReleaseEvent(QKeyEvent *event)
{
    _engine->onKeyUp(event);
}

void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
    _engine->onMouseMove(event);
}

void GLWidget::mousePressEvent(QMouseEvent *event)
{
    _engine->onMouseDown(event);
}

void GLWidget::wheelEvent(QWheelEvent *event)
{
    _engine->onWheelMove(event);
}

} // namespace Core
